Skip to content

Keep AOB scans inside their range and add scanForAOBInMainModule - #16

Merged
gynt merged 2 commits into
gynt:mainfrom
Krarilotus:fix/aob-scan-bounds
Nov 28, 2026
Merged

gynt merged 2 commits into
gynt:mainfrom
Krarilotus:fix/aob-scan-bounds

Conversation

@Krarilotus

@Krarilotus Krarilotus commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR

scanForAOB keeps exactly to its range. The new scanForAOBInMainModule(pattern) returns the first two matches in the main module's executable sections, so a caller can reject a pattern that is not unique. When it cannot read all of that code, it returns nil, nil, "incomplete" instead of a match. Version 1.6.0. UCP3 3.0.8 pins it (UnofficialCrusaderPatch/UnofficialCrusaderPatch3#156).

Included

  • AOB::FindPattern tests every start up to the last one where the pattern still fits (<=). Before, the last start was skipped, and a pattern longer than the length underflowed the loop bound.
  • AOB::Scan searches [min, max], both inclusive, so a match has to end at or before max.
    • Before, it read a region's full size from the start address, which could reach past the region's end. It also applied max per region, searching the region that holds max to its end.
    • Adjacent readable regions are searched as one range, so a match across a protection boundary (for example a page that was made writable for a patch) is still found.
    • Readable means committed, MEM_IMAGE, and neither PAGE_NOACCESS nor PAGE_GUARD. The image-only rule is unchanged.
  • AOB::FindInRange returns 0 for a pattern with no bytes instead of indexing an empty vector.
  • AOB::FindInMainModule and the Lua function scanForAOBInMainModule(pattern).
    • They search each section of the main module whose header has IMAGE_SCN_MEM_EXECUTE.
    • The headers are checked before they are read: the DOS and NT signatures, and the NT headers and section table lying inside the loaded image (GetModuleInformation's SizeOfImage) in readable memory. Invalid headers give "incomplete".
    • An executable section that is empty or does not lie inside the image gives "incomplete".
    • When a page of an executable section cannot be read (guard, no access, not committed), the result is nil, nil, "incomplete", because a duplicate there could not be ruled out. A guard page keeps its guard.
    • Otherwise they return the first match and the second one (nil for each one not found). Overlapping matches count.
    • The Lua function checks the pattern like scanForAOB.
  • README: the max meaning and the new function's results.
  • tests/test-aob.lua in the lunatest suite: an exact-size range, a range one byte too short, two matches, and no match.
  • Version 1.6.0 in Directory.Build.props and the nuspec, a minor version for the new Lua function.

Tested

  • MSBuild Release and Debug, x86 (v143): no warnings in the changed code.
  • tests/run.ps1 with the built Release/RPS.dll and Lua 5.4.6: 16 passed, 0 failed, including the 3 new tests.
  • Local x86 harnesses (not part of this PR) on the new scanner with VirtualProtected pages passed:
    • an exact-size range, the last legal start, and the upper and lower bound clipping a match;
    • an inverted range and a range shorter than the pattern;
    • a match in the last byte before a PAGE_NOACCESS page, with no read into it;
    • a guard page that is skipped and keeps its guard;
    • a match across a protection change;
    • in an executable section: one match, two matches, a guard page and a no-access page giving "incomplete" (the guard kept), and a complete result again after the page is restored;
    • the section's header changed in memory to a size past the image, a size of 0 and an address outside the image, each giving "incomplete", and a complete result again after it is restored.
  • UCP3 3.0.8 (a67270a) builds against this source, Release Win32.

Not verified

  • Not run in game. UCP3 3.0.8's core.AOBScanUnique uses scanForAOBInMainModule; the 3.0.8 modules rely on it to reject missing or ambiguous patterns.
  • The GitHub NuGet package is published by the tag workflow (v1.6.0). UCP3 restores RPS 1.6.0 from that feed; the package has not been published or restored yet.

@gynt

gynt commented Sep 14, 2026

Copy link
Copy Markdown
Owner

When did you encounter said bugs? Do you have some reproducible code that causes the bug?

scanForAOB searches [min, max] inclusively: a match has to end at or
before max, the last valid start is tested, and no read passes the end
of a readable region. Adjacent readable regions are searched as one
range.

scanForAOBInMainModule(pattern) returns the first and the second match
in the executable sections of the main module, so a caller can reject
an ambiguous signature. The PE headers and section ranges are checked
against the loaded image size before they are read. When the headers
are invalid, or an executable section is empty, lies outside the image
or cannot be read, it returns nil, nil, "incomplete", so an unread
duplicate is never reported as unique.

Version 1.6.0.
@Krarilotus Krarilotus changed the title Fix AOB scan bounds and expose main-image ambiguity detection Keep AOB scans inside their range and add scanForAOBInMainModule Nov 26, 2026
@Krarilotus

Copy link
Copy Markdown
Contributor Author

When did you encounter said bugs? Do you have some reproducible code that causes the bug?

This came up while modules checked that a pattern is unique inside a bounded window; Rebalancer added its own filter as a workaround. The old scanForAOB read each memory region to its end, so a match could start inside the range and end past max, or come from beyond it. A reproduction is in tests/test-aob.lua at 203a694: with an address first in readable code, scanForAOB("? ? ?", first, first + 1) must return nil, because the three-byte pattern doesn't fit in the two-byte range; the old scanner can return first. I haven't seen a game crash from it.

The larger reason for this PR is scanForAOBInMainModule: UCP3 3.0.8's core.AOBScanUnique is built on it, and the 3.0.8 modules use it to refuse missing or ambiguous patterns. So 3.0.8 needs RPS 1.6.0 tagged before it can build.

Posted by Claude on behalf of Krarilotus.

@gynt gynt left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good, except I prefer "scanForAOBsInMainModule" (notice the s for plural)

Comment thread CodeFunctions.h Resolved
Comment thread README.md Resolved
Comment thread RuntimePatchingSystem.cpp Resolved
Comment thread CodeFunctions.cpp Resolved
Comment thread tests/test-aob.lua Resolved
Comment thread tests/test-aob.lua Resolved
Comment thread tests/test-aob.lua Resolved
@gynt
gynt marked this pull request as ready for review November 27, 2026 19:34
@gynt
gynt merged commit 99ef018 into gynt:main Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants